package com.younion.action.manage;


import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;

import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.business.IFavourableTypeService;
import com.younion.vo.CustomerVO;
import com.younion.vo.FavourableTypeVO;

/**
 * @author jill.chan
*/
public class FavourableTypeAction extends BaseAction {

	protected static final Log logger = LogFactory.getLog(FavourableTypeAction.class);
	private IFavourableTypeService favourableTypeService = null;

	private FavourableTypeVO favourableTypeVO = new FavourableTypeVO();

	private List<FavourableTypeVO> favourableTypeList;;// 结果集列表
	/**
	 * 列表：翻页区；数据区
	 */
	
	@RequiresPermissions("listFavourableType")
	public String listFavourableType() {
		try {
			Map paramMap = null;
			if (favourableTypeVO != null) {
				if(favourableTypeVO.getCustomerId() != null){
					favourableTypeVO.setCustomer(new CustomerVO(favourableTypeVO.getCustomerId()));
				}else if(getSessionUser().getCustomer() != null){
					favourableTypeVO.setCustomer(getSessionUser().getCustomer());
				}
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(favourableTypeVO));
			}
			// 获取总记录数
			int total = favourableTypeService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			favourableTypeList = favourableTypeService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
			this.addLog(2, "查询优惠券类别列表", "查询优惠券类别列表", this.getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("addFavourableType")
	public String addFavourableType() {
		
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	@RequiresPermissions("editFavourableType")
	public String editFavourableType() {
		try {
			favourableTypeVO = favourableTypeService.getById(favourableTypeVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 *  保存动作
	 */
	@RequiresPermissions("saveFavourableType")
	public String saveFavourableType() {
		try {
			if(favourableTypeVO.getCustomerId() != null){
				favourableTypeVO.setCustomer(new CustomerVO(favourableTypeVO.getCustomerId()));
			}
			favourableTypeVO = favourableTypeService.save(favourableTypeVO);
			String id = favourableTypeVO.getId() == null ? "" : favourableTypeVO.getId().toString();
			String log = "".equals(id) ? "添加优惠券类别" : "更新优惠券类别";
			int type = "".equals(id) ? 3:4;
			this.addLog(type, log+id, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("deleteFavourableType")
	public String deleteFavourableType() {
		try {
			String pkId = request.getParameter("ids");
			favourableTypeService.remove(Arrays.asList(pkId.split(",")));
			String log = "删除优惠券类别";
			this.addLog(5, log+pkId, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	@RequiresPermissions("viewFavourableType")
	public String viewFavourableType() {
		try {
			String pkId = request.getParameter("ids"); 
			favourableTypeVO = favourableTypeService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public FavourableTypeVO getFavourableTypeVO() {
		return favourableTypeVO;
	}

	public void setFavourableTypeVO(FavourableTypeVO favourableTypeVO) {
		this.favourableTypeVO = favourableTypeVO;
	}

	public void setFavourableTypeService(IFavourableTypeService favourableTypeService) {
		this.favourableTypeService = favourableTypeService;
	}

	public List<FavourableTypeVO> getFavourableTypeList() {
		return favourableTypeList;
	}

	public void setFavourableTypeList(List<FavourableTypeVO> favourableTypeList) {
		this.favourableTypeList = favourableTypeList;
	}

}